<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车全选</title>
    <style>
        table {
            border-collapse: collapse;
            width: 300px;
            margin: 20px auto;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th><input type="checkbox" class="selectAll"></th>
                <th>商品名称</th>
                <th>商品价格</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="item"></td>
                <td>棒棒糖</td>
                <td>1.0</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="item"></td>
                <td>辣条</td>
                <td>2.0</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="item"></td>
                <td>汽水</td>
                <td>5.0</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="item"></td>
                <td>口香糖</td>
                <td>4.0</td>
            </tr>
        </tbody>
    </table>
</body>
<script>
    const selectAll = document.querySelector('.selectAll');
    const items = document.querySelectorAll('.item');

    selectAll.addEventListener('click', function () {
        items.forEach(checkbox => checkbox.checked = selectAll.checked);
    });

    items.forEach(checkbox => {
        checkbox.addEventListener('click', function () {
            const allChecked = Array.from(items).every(cb => cb.checked === true);
            selectAll.checked = allChecked;
        });
    });
</script>

</html>